package com.dxf.sysadmin.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxf.sysadmin.entity.*;
import com.dxf.sysadmin.service.*;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 前端layui.tree数据打包助手
 */
public class TreeHpler {

    /**
     * 菜单表标志，因为layui.tree的id不能重复所以加前缀
     */
    private static String menu = "A";
    /**
     * 操作表标志
     */
    private static String func = "B";


    /**
     * @Description: 获取角色权限tree
     * @Param sysMenuService 菜单
     * @Param sysFuncService 操作
     * @Return: java.util.List<com.dxf.sysadmin.utils.LayuiTree>
     */
    public static List<LayuiTree> getRoleTree(SysMenuService sysMenuService,
                                              SysFuncService sysFuncService) {
        List<LayuiTree> layuiTrees = new ArrayList<>();
        List<SysMenu> allMenu = sysMenuService.list();
        List<SysFunc> allfun = sysFuncService.list();
        //获取第一级菜单
//        List<SysMenu> listOne = sysMenuService.list(
//                new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, 0));
        List<SysMenu> listOne = allMenu.stream()
                .filter(x->x.getParentId().equals(0L)).collect(Collectors.toList());
        listOne.forEach(x -> {
            LayuiTree oneTree = new LayuiTree();
            oneTree.setDbid(x.getId());
            oneTree.setTitle(x.getName());
            oneTree.setId(menu + x.getId());
            oneTree.setSpread(true);
            //获取二级菜单
//            List<SysMenu> listTwo = sysMenuService.list(
//                    new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, x.getId()));
            List<SysMenu> listTwo = allMenu.stream()
                    .filter(m->m.getParentId().equals(x.getId())).collect(Collectors.toList());
            List<LayuiTree> layuiTwo = new ArrayList<>();
            listTwo.forEach(y -> {
                LayuiTree twoTree = new LayuiTree();
                twoTree.setDbid(y.getId());
                twoTree.setTitle(y.getName());
                twoTree.setId(menu + y.getId());
                twoTree.setSpread(true);

                //获取三级级菜单
//                List<SysFunc> listThree = sysFuncService.list(
//                        new QueryWrapper<SysFunc>().lambda().eq(SysFunc::getMenuId, y.getId()));
                List<SysFunc> listThree = allfun.stream()
                        .filter(m->m.getMenuId().equals(y.getId())).collect(Collectors.toList());
                List<LayuiTree> layuiThree = new ArrayList<>();
                listThree.forEach(k -> {
                    LayuiTree threeTree = new LayuiTree();
                    threeTree.setDbid(k.getId());
                    threeTree.setTitle(k.getName());
                    threeTree.setId(func + k.getId());

                    layuiThree.add(threeTree);
                });
                twoTree.setChildren(layuiThree);
                layuiTwo.add(twoTree);
            });
            oneTree.setChildren(layuiTwo);
            layuiTrees.add(oneTree);
        });
        return layuiTrees;
    }


    /**
     * @Description: 根据id获取角色tree树选中id集合
     * @Param id
     * @Param roleMenuService
     * @Param roleFuncService
     * @Return: java.util.List<java.lang.String>
     */
    public static List<String> getIds(Long id, RoleMenuService roleMenuService,
                                      RoleFuncService roleFuncService) {
        List<String> ids = new ArrayList<>();
        //菜单ids
/*        List<RoleMenu> menus = roleMenuService.list(new QueryWrapper<RoleMenu>()
                .lambda().eq(RoleMenu::getRoleId, id));
        menus.forEach(x->{
            ids.add(menu+x.getMenuId());
        });*/
        //操作ids
        List<RoleFunc> funcs = roleFuncService.list(new QueryWrapper<RoleFunc>()
                .lambda().eq(RoleFunc::getRoleId, id));
        funcs.forEach(y -> {
            ids.add(func + y.getFuncId());
        });
        return ids;
    }

    /**
     * @Description: 根据前端返回的选中数据，解析出权限id
     * @Param layuiTrees
     * @Return: java.util.Map<java.lang.String, java.util.List < java.lang.Long>>
     */
    public static Map<String, List<Long>> getWebIds(List<LayuiTree> layuiTrees) {
        List<Long> menuIds = new ArrayList<>();
        List<Long> funcIds = new ArrayList<>();
        layuiTrees.forEach(x -> {
            //选中父级id
            menuIds.add(x.getDbid());
            x.getChildren().forEach(y -> {
                menuIds.add(y.getDbid());
                y.getChildren().forEach(k -> {
                    funcIds.add(k.getDbid());
                });
            });
        });
        Map<String, List<Long>> map = new HashMap<>();
        map.put("menuIds", menuIds);
        map.put("funcIds", funcIds);
        return map;
    }

    /**
     * @Description: 获取菜单treeTable数据
     * @Param sysMenuService 菜单
     * @Param sysFuncService 操作
     * @Return: java.util.List<com.dxf.sysadmin.utils.MenuTree>
     */
    public static List<MenuTree> getMenuTree(SysMenuService sysMenuService,
                                             SysFuncService sysFuncService) {
        List<MenuTree> treeTable = new ArrayList<>();
        List<SysMenu> allMenu = sysMenuService.list();
        List<SysFunc> allfun = sysFuncService.list();
        //获取第一级菜单
//        List<SysMenu> listOne = sysMenuService.list(
//                new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, 0));
        List<SysMenu> listOne = allMenu.stream()
                .filter(x->x.getParentId().equals(0L)).collect(Collectors.toList());
        listOne.forEach(x -> {
            MenuTree oneTree = new MenuTree();
            BeanUtils.copyProperties(x, oneTree);
            oneTree.setTreeId(menu + x.getId());
            oneTree.setTreePid(x.getParentId() + "");
            oneTree.setIsMenu("0");

            treeTable.add(oneTree);

            //获取二级菜单
//            List<SysMenu> listTwo = sysMenuService.list(
//                    new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, x.getId()));
            List<SysMenu> listTwo = allMenu.stream()
                    .filter(m->m.getParentId().equals(x.getId())).collect(Collectors.toList());
            listTwo.forEach(y -> {
                MenuTree twoTree = new MenuTree();
                BeanUtils.copyProperties(y, twoTree);
                twoTree.setTreeId(menu + y.getId());
                twoTree.setTreePid(menu + x.getId());
                twoTree.setIsMenu("1");

                treeTable.add(twoTree);

                //获取三级操作
//                List<SysFunc> listThree = sysFuncService.list(
//                        new QueryWrapper<SysFunc>().lambda().eq(SysFunc::getMenuId, y.getId()));
                List<SysFunc> listThree = allfun.stream()
                        .filter(m->m.getMenuId().equals(y.getId())).collect(Collectors.toList());
                listThree.forEach(k -> {
                    MenuTree threeTree = new MenuTree();
                    BeanUtils.copyProperties(k, threeTree);
                    threeTree.setTreeId(func + k.getId());
                    threeTree.setTreePid(menu + y.getId());
                    threeTree.setIsMenu("2");

                    treeTable.add(threeTree);
                });
            });

        });
        return treeTable;
    }

    /**
     * @Description: 获取部门treetable数据
     * @Param deptService
     * @Return: java.util.List<com.dxf.sysadmin.utils.DeptTree>
     */
    public static List<DeptTree> getDeptTree(DeptService deptService) {
        List<DeptTree> treeTable = new ArrayList<>();
        //获取系
        List<Dept> listOne = deptService.list(
                new QueryWrapper<Dept>().lambda().eq(Dept::getParentId, 0));
        listOne.forEach(x -> {
            DeptTree one = new DeptTree();
            BeanUtils.copyProperties(x, one);
            treeTable.add(one);
            //获取教研室
            List<Dept> listTwo = deptService.list(
                    new QueryWrapper<Dept>().lambda().eq(Dept::getParentId, x.getId()));
            listTwo.forEach(y -> {
                DeptTree two = new DeptTree();
                BeanUtils.copyProperties(y, two);
                treeTable.add(two);
            });

        });
        return treeTable;
    }

    /**
     * @Description: 菜单添加时父级选择的SelectTree数据
     * @Param sysMenuService
     * @Return: java.util.List<com.dxf.sysadmin.utils.SelectTree>
     */
    public static List<SelectTree> getSelectTree(SysMenuService sysMenuService) {
        List<SelectTree> treeTable = new ArrayList<>();
        //获取第一级菜单
        List<SysMenu> listOne = sysMenuService.list(
                new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, 0));
        listOne.forEach(x -> {
            SelectTree one = new SelectTree();
            BeanUtils.copyProperties(x, one);

            //获取二级菜单
            List<SysMenu> listTwo = sysMenuService.list(
                    new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getParentId, x.getId()));
            List<SelectTree> layuitwo = new ArrayList<>();
            listTwo.forEach(y -> {
                SelectTree two = new SelectTree();
                BeanUtils.copyProperties(y, two);

                layuitwo.add(two);
            });
            one.setChildren(layuitwo);
            treeTable.add(one);
        });

        return treeTable;
    }
}
